package com.bac.bacplatform;

import java.util.HashMap;
import java.util.Map;

import android.content.Intent;
import android.drm.DrmStore.RightsStatus;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.bac.http.VolleyErrorHelper;
import com.bac.utils.Config;
import com.bac.utils.Util;

public class RegistActivity extends BaseActivity implements OnClickListener {
	private ImageButton clean_btn;
	private EditText phoneNumber, verify, password1, password2;
	private Button getVerify, registOk;

	private String phone_str, username_str, nickname_str, verifyid_str,
			password_str, verify_str;

	private TextView passwordVerify;

	private int count = 60;

	private Handler mHandler = new Handler();// 全局handler
	
	private boolean mIsGetCode = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_regist);

	}

	@Override
	public void init() {
		clean_btn = (ImageButton) findViewById(R.id.btn_clear_verify);
		getVerify = (Button) findViewById(R.id.regist_verify_btn);
		registOk = (Button) findViewById(R.id.regist_ok_btn);
		phoneNumber = (EditText) findViewById(R.id.regist_phoneNumber);
		password1 = (EditText) findViewById(R.id.regist_password);
		password2 = (EditText) findViewById(R.id.regist_password2);
		verify = (EditText) findViewById(R.id.regist_verify);
		passwordVerify = (TextView) findViewById(R.id.regist_password1_password2);

		clean_btn.setOnClickListener(this);
		getVerify.setOnClickListener(this);
		registOk.setOnClickListener(this);

		phoneNumber.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
			}

			@Override
			public void afterTextChanged(Editable arg0) {
				if (phoneNumber.getText().toString().length() > 0) {
					clean_btn.setVisibility(View.VISIBLE);
				} else {
					clean_btn.setVisibility(View.INVISIBLE);
				}
				
				if (phoneNumber.getText().toString().length() > 0 && password1.getText().toString().length() > 0 && password2.getText().toString().length() > 0) {
					if (count == 60) {
						getVerify.setBackgroundResource(R.drawable.red_btn);
						getVerify.setEnabled(true);
					}
				}
			}
		});
		
		password1.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
			}

			@Override
			public void afterTextChanged(Editable arg0) {
				if (phoneNumber.getText().toString().length() > 0 && password1.getText().toString().length() > 0 && password2.getText().toString().length() > 0) {
					if (count == 60) {
						getVerify.setBackgroundResource(R.drawable.red_btn);
						getVerify.setEnabled(true);
					}
				}
			}
		});
		
		password2.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
			}

			@Override
			public void afterTextChanged(Editable arg0) {
				if (phoneNumber.getText().toString().length() > 0 && password1.getText().toString().length() > 0 && password2.getText().toString().length() > 0) {
					if (count == 60) {
						getVerify.setBackgroundResource(R.drawable.red_btn);
						getVerify.setEnabled(true);
					}
				}
				verifyPassword();
			}
		});
		
		verify.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
			}

			@Override
			public void afterTextChanged(Editable arg0) {
				if (mIsGetCode && verify.getText().toString().length() > 0) {
					registOk.setEnabled(true);
					registOk.setBackgroundResource(R.drawable.blue_btn);
				}else{
					registOk.setEnabled(false);
					registOk.setBackgroundResource(R.drawable.btn_off);
				}
			}
		});
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_clear_verify:
			phoneNumber.setText("");
			break;
		case R.id.regist_verify_btn:
			if (verifyInput(3)) {
				regist();
				getVerify.setBackgroundResource(R.drawable.white_corner);
				getVerify.setEnabled(false);
				new Thread(new ClassCut()).start();// 倒计时
			} else {
				return;
			}
			break;
		case R.id.regist_ok_btn:
			if (verifyInput(4))
				regist_active();
			break;
		default:
			break;
		}
	}

	// 注册等待返回验证码
	private void regist() {
		/**
		 * 成功返回结果
		 */
		Listener<String> sL = new Response.Listener<String>() {
			@Override
			public void onResponse(String response) {

				dismissLoadingProgress();
				Log.i(getClass().getName()+"----", response.toString());
				verifyid_str = response.toString();
				mIsGetCode = true;
				showToast("短信验证码已发送");
			}
		};
		phone_str = phoneNumber.getText().toString();
		username_str = phoneNumber.getText().toString();
		nickname_str = phoneNumber.getText().toString();
		password_str = password1.getText().toString();
		if (phone_str.length() <= 0) {
			showToast(R.string.phone_none);
			return;
		} else if (password_str.length() <= 0) {
			showToast(R.string.password_none);
			return;
		}
		showLoadingProgress();
		String path = Config.URL_REGIST;
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", username_str);
		map.put("nickname", nickname_str);
		map.put("phone", phone_str);
		map.put("password", password_str);
		Util.httpStringPost(path, sL, this, map);

	}

	// 使用验证码解锁账号
	private void regist_active() {
		
		ErrorListener errorListener = new ErrorListener() {
			@Override
			public void onErrorResponse(VolleyError error) {
				dismissLoadingProgress();
				registOk.setEnabled(true);
				registOk.setBackgroundResource(R.drawable.blue_btn);
				VolleyErrorHelper.handleServerError(error, RegistActivity.this);
			}
		};
		
		/**
		 * 成功返回结果
		 */
		Listener<String> sL = new Response.Listener<String>() {
			@Override
			public void onResponse(String response) {

				dismissLoadingProgress();
				Log.i(getClass().getName()+"----success", response.toString());

				Intent intent = new Intent(RegistActivity.this,
						LoginActivity.class);
				startActivityIn(intent);
			}
		};
		verify_str = verify.getText().toString();
		if (verify_str.length() <= 0) {
			showToast(R.string.verify_none);
			return;
		}
		showLoadingProgress();
		registOk.setEnabled(false);
		registOk.setBackgroundResource(R.drawable.btn_off);
		String path = Config.URL_REGIST_ACTIVE;
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", verifyid_str);
		map.put("code", verify_str);
		Util.httpStringPost(path, sL, errorListener, map);
	}

	private boolean verifyInput(int i) {
		if (phoneNumber.getText().toString().length() == 0) {
			showToast(R.string.phone_none);
			return false;
		} else if (password1.getText().toString().length() == 0) {
			showToast(R.string.password_none);
			return false;
		} else if (password2.getText().toString().length() == 0) {
			showToast(R.string.password_none);
			return false;
		} else if (i == 4 && verify.getText().toString().length() == 0) {
			showToast(R.string.verify_none);
			return false;
		} else if (!password1.getText().toString()
				.equals(password2.getText().toString())) {
			showToast(R.string.password1_password2);
			passwordVerify.setVisibility(View.VISIBLE);
			return false;
		} else {
			return true;
		}
	}

	private void verifyPassword() {
		if (password1.getText().toString()
				.equals(password2.getText().toString())) {
			passwordVerify.setVisibility(View.INVISIBLE);
		}
	}

	class ClassCut implements Runnable {
		@Override
		public void run() {
			while (count > 0) {
				count--;
				mHandler.post(new Runnable() {
					@Override
					public void run() {

						getVerify.setText("剩余:" + count + "\"");
					}
				});
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			mHandler.post(new Runnable() {
				@Override
				public void run() {
					getVerify.setText(R.string.regist_helpverify);
					getVerify.setBackgroundResource(R.drawable.red_btn);
					getVerify.setEnabled(true);
				}
			});
			count = 60;
		}
	}
}
